package keter.combofs.path.impl;

import keter.combofs.path.PathMaker;
import keter.combofs.util.IOUtil;
import keter.combofs.util.PathMakerUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 文件系统子目录生成默认规则
 * Created by gulx@neusoft.com on 2019/11/11.
 */
@Component
public class PathMakerFSImpl implements PathMaker {

    @Value("${combofs.root}")
    protected String root;

    /**
     * 在文件系统上创建目录
     * @param fileName
     * @return
     */
    @Override
    public String makePath(String fileName) {
        String path = PathMakerUtil.caculatePath(fileName);
        // 创建子目录
        IOUtil.ensureExists(root + path);
        return path;
    }

    @Override
    public String getPath(Long id) {
        return PathMakerUtil.caculatePath(id.toString());
    }

    @Override
    public String getUri(Long id) {
        return root + makePath(id.toString())+id.toString();
    }

}
